package by.bsu.touragency.tag;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import by.bsu.touragency.db.beans.Tour;
import by.bsu.touragency.db.dao.TourDAO;


public class SearchTag extends BodyTagSupport{

	private static final long serialVersionUID = -4803241311077357656L;
	private static Logger logger=Logger.getLogger("log");
	private String login;
	private ArrayList<Tour> arrT;
    private TourDAO t;

	public int getTourPrice(int num) {
		return arrT.get(num).getPrice();
	}
	
	public String getTourPlace(int num) {
		String str;
		str = arrT.get(num).getHotel().getName()+", ";
		str += arrT.get(num).getHotel().getCity().getCityName()+", ";
		str +=arrT.get(num).getHotel().getCity().getCountry().getCountryName();
		return str;
	}
	
	public String getTourChildren(int num) {
		String str;
		str = arrT.get(num).getHotel().getPeople().getChildren()+ "";

		return str;
	}
	
	public String getTourAdults(int num) {
		String str;
		str = arrT.get(num).getHotel().getPeople().getAdults()+"";
		return str;
	}
	
	public String getTourEat(int num) {
		String str;
		str = arrT.get(num).getHotel().getEat().getType();;
		return str;
	}
	
	public String getTourHotel(int num) {
		String str;
		str = arrT.get(num).getHotel().getName();
		return str;
	}
	
	public String getTourCountry(int num) {
		String str;
		str = arrT.get(num).getHotel().getCity().getCountry().getCountryName();
		return str;
	}
	
	public String getTourCity(int num) {
		String str;	
		str = arrT.get(num).getHotel().getCity().getCityName();
		return str;
	}

	public String getCityFrom(int num) {
		String str;
		str = arrT.get(num).getTransport().getCityFrom().getCityName()+" ";
		return str;
	}
	
	public String getTourStar(int num) {
		String str;
		str = arrT.get(num).getHotel().getStar().getName();
		return str;
	}
	
	public String getTourDateTo(int num) {
		String str;
		str = arrT.get(num).getDateTo().getTime()+" ";
		return str;
	}
	
    public String getTourDateFrom(int num) {
		String str;
		str = arrT.get(num).getDateFrom().getTime()+" ";
		return str;
	}
	
	public int doStartTag() throws JspTagException {
		logger.info("All not booked tours tag do start");	
		t = new TourDAO();
		arrT = new ArrayList<Tour>();
		arrT = t.readNB();
		return EVAL_BODY_INCLUDE;
	}

	public int doAfterBody() throws JspTagException {
		logger.info("All not booked tours tag After body");	
			try {
				pageContext.getOut().write("var Data = new createArray("+arrT.size()+");");//size of array from base
				for (int i=0;i<arrT.size();i++)
				{
					pageContext.getOut().write("Data["+(i+1)+"] = \"");
					
					pageContext.getOut().write(getTourHotel(i)+";");
				
					pageContext.getOut().write(getTourCity(i)+";");
					
					pageContext.getOut().write(getTourCountry(i)+";");
					
					pageContext.getOut().write(getTourEat(i)+";");
					
					pageContext.getOut().write(getTourStar(i)+";");
					
					pageContext.getOut().write(getTourDateTo(i)+";");
					
					pageContext.getOut().write(getTourDateFrom(i)+";");
					
					pageContext.getOut().write(getCityFrom(i)+";");
					
					pageContext.getOut().write(getTourChildren(i)+";");
					
					pageContext.getOut().write(getTourAdults(i)+";");
					
					pageContext.getOut().write(getTourPrice(i)+";");
					
					pageContext.getOut().write(arrT.get(i).getId()+"");
					
					pageContext.getOut().write("\";");
				}
				
			} catch (IOException e) {
				throw new JspTagException(e.getMessage());
			}
			return SKIP_BODY;
	}

	public int doEndTag() throws JspTagException {
		return SKIP_BODY;
	}
	
	public void setUser(String user) {
		this.setLogin(new String(user));
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}
}
